﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Profile;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;
using Facade;
using System.Collections;


namespace IronOne.SalesOne.SalesOneWeb.UserManagement
{
    public partial class AddGroup : SOSecurityPage
    {
        private string action = "create";

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                ValidateUserCredentials(Request.Url.LocalPath);

                if (!String.IsNullOrEmpty(Request.QueryString["a"]))
                {
                    action = Request.QueryString["a"].ToString();

                }



                if (action == "v")
                {
                    //lblHeader.Text = "View group";
                    this.View();

                }
                else if (action == "create" && !IsPostBack)
                {
                    txtEffectiveFrom.Text = DateTime.Now.ToShortDateString();
                    txtEffectiveTo.Text = DateTime.Now.ToShortDateString();
                }

                else if (action == "u")
                {
                    //lblHeader.Text = "Update group";
                    if (!IsPostBack)
                    {
                        this.Update();
                    }

                }
            }
            catch (Exception)
            {
                
            }
            
            
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {

            try
            {
                if (!(Convert.ToDateTime(txtEffectiveFrom.Text).CompareTo(DateTime.Today) >= 0))
                {
                    MassageSummery("IIS534");
                }
                else if (!(Convert.ToDateTime(txtEffectiveTo.Text).CompareTo(Convert.ToDateTime(txtEffectiveFrom.Text)) >= 0))
                {
                    MassageSummery("IIS535");
                }
                else if (txtgroupCode.Text.Length > 4)
                {
                    MassageSummery("IIS541");
                }
                else if (!String.IsNullOrEmpty(Request.QueryString["a"]))
                {
                    //it is update so query string is not empty

                    if (Session["groupId"] != null)
                    {

                        StructureFacade fac = new StructureFacade();
                        SalesOneGroup grp = fac.GetGroup(Convert.ToInt32(Session["groupId"].ToString()));
                        //new SalesOneGroup();

                        

                        grp.GroupId = Convert.ToInt32(Session["groupId"].ToString());

                        grp.Code = txtgroupCode.Text;
                        grp.Name = txtGroupName.Text;
                        grp.EffectFrom = txtEffectiveFrom.Text;
                        grp.EffectTo = txtEffectiveTo.Text;

                        int i = fac.UpdateGroup(grp);
                        if (i == 1)
                        {
                            Session["sccGrp"] = "Successfully updated.";
                        }
                        else
                        {
                            Session["failGrp"] = "Failed to update.";
                        }


                        Session.Remove("groupId");
                    }


                    Response.Redirect("~/UserManagement/ManageGroups.aspx");
                }
                else
                {
                    StructureFacade fac = new StructureFacade();
                    SalesOneGroup grp = new SalesOneGroup();
                    grp.Code = txtgroupCode.Text;
                    grp.Name = txtGroupName.Text;
                    grp.EffectFrom = txtEffectiveFrom.Text;
                    grp.EffectTo = txtEffectiveTo.Text;
                    grp.LastUpdated = DateTime.Now.ToShortDateString();

                    try
                    {
                        fac.InserGroup(grp);
                        Session["sccGrp"] = "Successfully inserted.";
                    }
                    catch (Exception)
                    {
                        Session["failGrp"] = "Failed to insert";
                    }


                    Response.Redirect("~/UserManagement/ManageGroups.aspx",false);
                }

            }
            catch (Exception)
            {

            }

            
            
        }

        private void View()
        {
            try
            {
                lbAs1.Visible = false;
                lbAs2.Visible = false;
                lbAs3.Visible = false;
                lbAs4.Visible = false;

                txtGroupName.Visible = false;
                txtgroupCode.Visible = false;
                txtEffectiveFrom.Visible = false;
                txtEffectiveTo.Visible = false;
                RequiredFieldValidator1.Enabled = false;
                RequiredFieldValidator2.Enabled = false;
                Image1.Visible = false;
                Image4.Visible = false;

                if (Session["groupId"] != null)
                {
                    StructureFacade fac = new StructureFacade();
                    string grp = Session["groupId"].ToString();
                    SalesOneGroup gr = fac.GetGroup(Convert.ToInt32(grp));

                    lblCode.Text = gr.Code;
                    lblName.Text = gr.Name;
                    lblEffectiveFrm.Text = gr.EffectFrom;
                    lblEffectiveTo.Text = gr.EffectTo;

                    btnCreate.Visible = false;
                    Session.Remove("groupId");
                }
            }
            catch (Exception)
            {

            }
            

                       
        }

        private void Create()
        {
            lblCode.Visible = false;
            lblName.Visible = false;
            lblEffectiveFrm.Visible = false;
            lblEffectiveTo.Visible = false;
        }

        private void Update()
        {
            try
            {
                lblCode.Visible = false;
                lblName.Visible = false;
                lblEffectiveFrm.Visible = false;
                lblEffectiveTo.Visible = false;

                if (Session["groupId"] != null)
                {
                    StructureFacade fac = new StructureFacade();
                    string grp = Session["groupId"].ToString();
                    SalesOneGroup gr = fac.GetGroup(Convert.ToInt32(grp));

                    txtgroupCode.Text = gr.Code;
                    txtGroupName.Text = gr.Name;
                    txtEffectiveFrom.Text = gr.EffectFrom;
                    txtEffectiveTo.Text = gr.EffectTo;

                    btnCreate.Text = "Update";
                }
            }
            catch (Exception)
            {
            }
            
            


        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            //RequiredFieldValidator1.c = false;
            //RequiredFieldValidator2.Enabled = false;
            
            Response.Redirect("~/UserManagement/ManageGroups.aspx");
        }

        //private void Delete()
        //{
        //    StructureFacade fac = new StructureFacade();
        //    string grp = Session["/UserManagement/ManageGroups.aspxview"].ToString();
        //    SalesOneGroup gr = fac.GetGroup(Convert.ToInt32(grp));

        //    fac.DeleteGroup(gr);

        //    Response.Redirect("~/UserManagement/ManageGroups.aspx");
        //}
    }
}
